//
//  AFHttpService.m
//  TyCommunityClientC
//
//  Created by Johnny on 15/12/8.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "AFHttpService.h"
#import "TSHeader.h"
#import "ComMethod.h"

#define HttpTimeOut     15

@interface AFHttpService ()

@end

static AFHttpService *_httpService = nil;
@implementation AFHttpService

+ (AFHttpService *)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _httpService = [[AFHttpService alloc] initWithBaseURL:[NSURL URLWithString:@"http://dispatch.tengyunmall.cn:8181"]];
        _httpService.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        _httpService.requestSerializer = [AFJSONRequestSerializer serializer];
        _httpService.responseSerializer = [AFJSONResponseSerializer serializer];
//        [_httpService.requestSerializer setValue:[ComMethod getSystemVersion] forHTTPHeaderField:@"systemVersion"];
//        [_httpService.requestSerializer setValue:[ComMethod getAppVersion] forHTTPHeaderField:@"terminalVersion"];
//        [_httpService.requestSerializer setValue:[ComMethod getOpenUDID] forHTTPHeaderField:@"imei"];
//        [_httpService.requestSerializer setValue:@"0" forHTTPHeaderField:@"terminalType"];
        _httpService.requestSerializer.timeoutInterval = HttpTimeOut;
    });
    return _httpService;
}

- (void)POST:(NSString *)URLString Dictionary:(NSDictionary *)dic success:(void (^)(ResponseHeader *response))success failure:(void (^)(NSString *errorMsg))failure {
    //对parametrs进一步封装
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    DLog(@"%@",mutDic);
    [_httpService POST:URLString parameters:mutDic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DLog(@"%@，%@",responseObject,[responseObject objectForKey:@"msg"]);
        ResponseHeader *response = [ResponseHeader objectWithKeyValues:responseObject];
        success(response);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"%@",error);
        failure(@"网络请求失败，请检查您的网络！");
    }];
}

/*
- (void)uploadImageData:(NSData *)imageData success:(void (^)(ResponseHeader *))success failure:(void (^)(NSString *))failure {
    [_httpService.requestSerializer setValue:@"01" forHTTPHeaderField:@"type"];
    NSString *filePath = [ComMethod getBundlePathWithFileName:@"image.jpg"];
    if ([imageData writeToFile:filePath atomically:YES]) {
        [_httpService POST:@"/business_app/common/uploadImg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:imageData name:@"uploadPic" fileName:filePath mimeType:@"image/jpg"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            DLog(@"%@",responseObject);
            ResponseHeader *response = [ResponseHeader mj_objectWithKeyValues:responseObject];
            if (response.code == 9) {//表示账号被其他人登陆
                CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:nil message:@"你的账号已在另一台设备上登录，请重新登录" delegate:self cancelButtonTitle:@"确定" sureButtonTitle:nil];
                [alert show];
            }
            success(response);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            DLog(@"%@",error);
            failure(@"网络请求失败，请检查您的网络！");
        }];
    }
}
*/
@end
